package com.example.notepad.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.notepad.R;
import com.example.notepad.bean.Notepad;

import java.util.List;

public class NotepadAdapter extends RecyclerView.Adapter<NotepadAdapter.ViewHolder> {
    private List<Notepad> notepads;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public NotepadAdapter(List<Notepad> notepads) {
        this.notepads = notepads;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notepad, parent, false);
        return new ViewHolder(view, onItemClickListener, onItemLongClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Notepad notepad = notepads.get(position);
        holder.tvContent.setText(notepad.getContent());
        holder.tvTime.setText(notepad.getTime());
    }

    @Override
    public int getItemCount() {
        return notepads.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvContent;
        TextView tvTime;

        public ViewHolder(@NonNull View itemView, final OnItemClickListener onItemClickListener,
                          final OnItemLongClickListener onItemLongClickListener) {
            super(itemView);

            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, getAdapterPosition());
                    }
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if(onItemLongClickListener != null) {
                        onItemLongClickListener.onItemLongClick(v, getAdapterPosition());
                    }
                    return true;
                }
            });

        }
    }

    // 回调事件的接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    // 回调事件的接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }
}
